const labelService = require("../service/label.service");


const verifyExists = async (ctx, next) => {
    //取出要添加的所有标签
    const { labels } = ctx.request.body;
    // console.log(1, labels);

    const newLabels = [];
    for (let name of labels) {
        // console.log(2, name)
        const labelResult = await labelService.getLabelName(name);
        const label = {name};
        if (!labelResult) {
            //创建标签
            const result = await labelService.create(name);
            label.id = result.insertId;
        } else {
            label.id = labelResult.id;
        }
        newLabels.push(label);
    }
    ctx.labels = newLabels;
    // console.log(ctx.labels)

    await next();
}

module.exports = {
    verifyExists
}